<?php

namespace App\Admin\Controllers\Pets;

use App\Models\Pets\WxPetsAdoptionApply;
use App\Wen\Utils\Settings;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxPetsAdoptionApplyController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxPetsAdoptionApply(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('users', '头像')->display(function ($v) {
                return '<img src="' . ($this->user['user_avatar'] ?? Settings::get('img_default', 'https://img.mini.minisns.cn/icons/dafault.png'))  . '"  style="border-radius:50px;width:30px;" />';
            })->width('50px');
            $grid->column('user')->display(function ($v) {
                if($v && $v['user_name']){
                    return '<a target="_blank" href="'.admin_url('users?id='.$v['id']).'">'.$v['user_name'].'</a>' ?? '用户已删除';
                }
                return '用户已删除';
            })->width('100px');
            $grid->column('pet_adoption_id');
            $grid->column('gender');
            $grid->column('is_experience');
            $grid->column('marital_status');
            $grid->column('housing_status');
            $grid->column('working_status');
            $grid->column('job');
            $grid->column('birthday');
            $grid->column('income');
            $grid->column('province');
            $grid->column('city');
            $grid->column('district');
            $grid->column('mobile');
            $grid->column('wechatId');
            $grid->column('remark');
            $grid->column('status');
//            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });

            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->disableBatchActions();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxPetsAdoptionApply(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('pet_adoption_id');
            $show->field('gender');
            $show->field('is_experience');
            $show->field('marital_status');
            $show->field('housing_status');
            $show->field('working_status');
            $show->field('job');
            $show->field('birthday');
            $show->field('income');
            $show->field('province');
            $show->field('city');
            $show->field('district');
            $show->field('mobile');
            $show->field('wechatId');
            $show->field('remark');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxPetsAdoptionApply(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('pet_adoption_id');
            $form->text('gender');
            $form->text('is_experience');
            $form->text('marital_status');
            $form->text('housing_status');
            $form->text('working_status');
            $form->text('job');
            $form->text('birthday');
            $form->text('income');
            $form->text('province');
            $form->text('city');
            $form->text('district');
            $form->text('mobile');
            $form->text('wechatId');
            $form->text('remark');
            $form->text('status');

            $form->display('created_at');
            $form->display('updated_at');

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
